package org.jdcenter.server.xb.metadata;
/**
 * @author miao
 * @since 2008-12-03
 *
 */
import org.jdcenter.server.xb.visitors.AbstractVisitor;

public class BeansMateData implements MateDataList{
	
	protected MateDataSet dataSet = null;
	
	protected BeanMateData mateData = null;
	
	public BeansMateData(){
		
	}
	
	public BeansMateData(MateDataSet dataSet){
		this.dataSet = dataSet;
	}

	@Override
	public void accept(AbstractVisitor dm) {
		dm.visitMateData(this);
		if(mateData != null){
			mateData.accept(dm);
		}
	}

	@Override
	public <T extends MateDataList> T getMateData() {
		return (T)mateData;
	}
	
	public void checkMateData(MateDataList mateData) throws MateDataException{
		if(dataSet == null){
			throw new MateDataException("mateData must be "+getClass().getName()
					+" when dataSet is null");
		}
	}

	@Override
	public <T extends MateDataList> void setMateData(T mateData) throws MateDataException {
		if(mateData.getClass() == getClass() 
				|| mateData.getClass().getSuperclass() == getClass()){
			this.mateData = (BeanMateData)mateData;
		}else{
			checkMateData(mateData);
			dataSet.setChildMateData(mateData, 
					getClass().getSuperclass()==Object.class?getClass():getClass().getSuperclass());
		}
	}

	@Override
	public <T extends MateDataList> void addMateData(T mateData)
			throws MateDataException {
		if(this.mateData == null){
			setMateData(mateData);
		}else{
			this.mateData.addMateData(mateData);
		}
	}

}
